package com.mjy.integrationtest.manager;

import com.mjy.integrationtest.util.MathUtil;
import com.mjy.integrationtest.model.Trade;
import java.time.LocalDateTime;
import java.util.UUID;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;
import org.springframework.stereotype.Service;

@Service("messageManager")
public class MessageManager {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private MessageChannel pacsBzOrderMessageChannel;

    public void send() {
        pacsBzOrderMessageChannel.send(MessageBuilder.withPayload("").build());
    }

    public int getRandomInt() {
        return MathUtil.randomInt(100);
    }

    public Trade newTrade(int type) {
        return Trade.builder().pfDocNo(UUID.randomUUID().toString())
                .businessUnitCode(UUID.randomUUID().toString()).createTime(
                        LocalDateTime.now()).type(type % 2).build();
    }

    @SneakyThrows
    public Trade createTrade(Trade trade) {
        Thread.sleep(MathUtil.randomInt(10));
        return mongoTemplate.insert(trade);
    }

}
